package com.restkeeper.store.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.restkeeper.store.entity.CreditLogs;
import com.restkeeper.store.mapper.CreditLogsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>
 * 挂账记录 服务实现类
 * </p>
 */
@Slf4j
@Component
@Service(version = "1.0.0",protocol = "dubbo")
public class CreditLogsServiceImpl extends ServiceImpl<CreditLogsMapper, CreditLogs> implements ICreditLogsService {

    /**
     * 分页查询挂账订单明细
     * @param pageNo
     * @param pageSize
     * @param creditId
     * @return
     */
    @Override
    public IPage<CreditLogs> queryPageByCreditId(long pageNo, long pageSize, String creditId, Date startTime, Date endTime) {

        // 构建分页条件
        IPage<CreditLogs> page = new Page<>(pageNo, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<CreditLogs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CreditLogs::getCreditId, creditId)
                .between(startTime != null && endTime != null,
                        CreditLogs::getLastUpdateTime, startTime, endTime)
                .orderByDesc(CreditLogs::getLastUpdateTime);

        // 获取分页数据并返回
        return this.page(page,queryWrapper);
    }
}
